﻿/*
 * $Header: /opt/javacvs/util/src/main/java/com/uusee/util/Base64Util.java,v 1.1 2008/07/17 05:12:17 wangrui Exp $
 * $Revision: 1.1 $
 * $Date: 2008/07/17 05:12:17 $
 * ============================================================================
 *
 * Copyright (c) 2008 Beijing uusee Tech CO.,  All rights reserved.
 *
 * ============================================================================
 */
package com.uusee.util;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;

/**
 * Base64编码算法工具类
 * 
 * @author <a href="mailto:du_yi@bbn.cn">duyi</a>
 * 
 * Create Time: 2008-4-30 上午09:56:48
 */
public class Base64Util {
	public static void main(String[] args){
		//EZVGjVWPuUfKc
		System.out.println(encode("33042630"));
	}
	private Base64Util() {

	}

	/**
	 * 编码
	 * 
	 * @param originStr
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String encode(String originStr) {
		String result = "";
		try {
			result = Base64Util.encode(originStr, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// UTF-8是合法charset,不会抛出异常,所以将UnsupportedEncodingException隐藏
		}
		return result;
	}

	/**
	 * 解码
	 * 
	 * @param originStr
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String decode(String originStr) {
		String result = "";
		try {
			result = Base64Util.decode(originStr, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// UTF-8是合法charset,不会抛出异常,所以将UnsupportedEncodingException隐藏
		}
		return result;
	}

	public static String encode(String originStr, String charset) throws UnsupportedEncodingException {
		return new String(Base64.encodeBase64(originStr.getBytes(charset)), charset);
	}

	public static String decode(String originStr, String charset) throws UnsupportedEncodingException {
		return new String(Base64.decodeBase64(originStr.getBytes(charset)), charset);
	}
}
